package rgbos.forum.GUI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import rgbos.forum.domain.Auditor;

public class GuiClient implements Runnable{



	private String host;
	private int port;
	private final String END_MESSAGE;
	//private final BufferedReader userReader = new BufferedReader(new InputStreamReader(System.in));
	private Socket clientSocket; // the connection socket
	private PrintWriter socketWriter;
	private BufferedReader socketReader;

	public GuiClient(String host, int port) {
		super();
		this.host = host;
		this.port = port;
		clientSocket = null; // the connection socket
		socketWriter = null;
		socketReader = null;
		END_MESSAGE = "benny gay";
	}
	//@Override

	public void run() {

		Auditor.Log("Connecting to " + host + ":" + port,this.getClass());

		// Trying to connect to a socket and initialize an output stream
		try {
			clientSocket = new Socket(host, port); // host and port
      		socketWriter = new PrintWriter(clientSocket.getOutputStream(), true);
    	} catch (UnknownHostException e) {
    	//	Auditor.Log("Unknown host: " + host,"severe",this.getClass());
      		System.exit(1);
	    } catch (IOException e) {
	    	Auditor.Log("Couldn't get output to " + host + " connection","severe",this.getClass());
			System.exit(1);
    	}

    	// Initialize an input stream
    	try {
    		socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    	} catch (IOException e) {
    		Auditor.Log("Couldn't get input to " + host + " connection","severe",this.getClass());
			System.exit(1);
    	}


    	Auditor.Log("Connected to server!",this.getClass());


	}

	public String invokeCmd(String cmd){
		this.socketWriter.println(cmd);
		String answer = null;
		try {
			answer=this.socketReader.readLine();
		} catch (IOException e) {
			//e.printStackTrace();
		}
		return answer;
	}


}
